<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>申报人员配置</span>
      </div>

      <el-form
        :inline="true"
        v-show="showSearch"
        :model="queryParams"
        label-width="68px"
      >
        <el-form-item label="人员名称" prop="nickname">
          <el-input
            v-model="queryParams.nickname"
            placeholder="请输入人员"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="部门" prop="dept">
          <el-input
            v-model="queryParams.dept"
            placeholder="请输入部门"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="职位" prop="post">
          <el-input
            v-model="queryParams.post"
            placeholder="请输入职位"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['work:declarer:add']"
            >新增申报人员</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="resList" stripe>
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">
            {{
              (queryParams.pageNum - 1) * queryParams.pageSize +
              scope.$index +
              1
            }}
          </template>
        </el-table-column>
        <el-table-column label="头像">
          <template slot-scope="scope">
            <img :src="prefix + scope.row.avatar" width="40" />
          </template>
        </el-table-column>
        <el-table-column label="人员姓名" prop="nickname" />
        <el-table-column label="部门" prop="dept" />
        <el-table-column label="职位" prop="post" />
        <el-table-column label="负责人" prop="principalName" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-hasPermi="['work:declarer:edit']"
              @click="handleUpdate(scope.row)"
              >编辑负责人</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              v-hasPermi="['work:declarer:remove']"
              @click="handleDelete(scope.row)"
              >移除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 申报人员配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form :model="optionQueryParams" label-width="50px">
        <el-row>
          <el-col :span="10">
            <el-form-item label="人员" prop="nickname">
              <el-input
                v-model="optionQueryParams.nickname"
                placeholder="请输入人员名称"
                clearable
                size="small"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="部门" prop="deptId">
              <treeselect
                v-model="optionQueryParams.deptId"
                :options="deptOptions"
                :show-count="true"
                placeholder="请选择归属部门"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4" style="text-align: center">
            <el-button
              type="text"
              icon="el-icon-search"
              size="mini"
              @click="getDeclarers"
              >搜索</el-button
            >
          </el-col>
        </el-row>
      </el-form>
      <el-table
        :data="declarerOptions"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column property="date" label="序号" /> -->
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column property="nickname" label="姓名" />
        <el-table-column property="dept" label="部门" />
        <el-table-column property="post" label="职位" />
      </el-table>
      <pagination
        v-show="optionTotal > 0"
        :total="optionTotal"
        layout="total, prev, pager, next"
        :pagerCount.sync="optionQueryParams.pagerCount"
        :page.sync="optionQueryParams.pageNum"
        :limit.sync="optionQueryParams.pageSize"
        @pagination="getDeclarers"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="flag" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改人员-责任人 -->
    <el-dialog
      :title="editTitle"
      :visible.sync="editOpen"
      width="500px"
      append-to-body
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="editRules"
        label-width="100px"
      >
        <el-form-item label="责任人姓名" prop="userId">
          <el-select
            v-model="editForm.principalId"
            clearable
            filterable
            placeholder="请选择人员"
            style="width: 100%"
          >
            <el-option
              v-for="item in userOptions"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
              :hidden="item.status == 1"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editSubmitForm">确 定</el-button>
        <el-button @click="editCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDeclarer,
  addDeclarer,
  getDeclarer,
  updateDeclarer,
  delDeclarer,
  optionSelect as getDeclarers
} from '@/api/work/declarer'
import { optionSelect as getUsers } from '@/api/system/user'
import { treeselect } from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'Declarer',
  components: { Treeselect },
  data() {
    return {
      // 图片前缀
      prefix: process.env.VUE_APP_BASE_API,
      // 查询参数
      queryParams: {},
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      flag: true,
      // 我的填报
      resList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: null,
        dept: null,
        post: null
      },
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 部门树选项
      deptOptions: [],
      // 申报人员选项
      declarerOptions: [],
      // 弹框查询参数
      optionQueryParams: {
        pageNum: 1,
        pageSize: 10,
        pagerCount: 5
      },
      // 表单参数
      editForm: {},
      // 用户选项
      userOptions: [],
      // 弹框申报人员选项总条数
      optionTotal: 0,
      // 编辑责任人标题
      editTitle: '',
      // 是否显示弹出层
      editOpen: false,
      // 表单校验
      editRules: {
        userId: [{ required: true, message: '人员不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 初始化 */
    getList() {
      this.loading = true
      listDeclarer(this.queryParams).then((response) => {
        this.resList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.fillDate = ''
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        nickname: null,
        dept: null,
        post: null
      }
      this.handleQuery()
    },
    /** 新增申报人员 */
    handleAdd() {
      this.optionQueryParams = {
        pageNum: 1,
        pageSize: 10,
        pagerCount: 5,
        nickname: undefined,
        deptId: undefined
      }
      this.open = true
      this.title = '添加申报人员信息'
      this.getTreeselect()
      this.getDeclarers()
    },
    /** 获取未申报人员 */
    getDeclarers() {
      getDeclarers(this.optionQueryParams).then((response) => {
        this.declarerOptions = response.rows
        this.optionTotal = response.total
      })
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data
      })
    },
    /** 移除 */
    handleDelete(row) {
      if (row) {
        const declarerId = row.declarerId
        this.$modal
          .confirm('是否确认删除人员 "' + row.nickname + '" 的数据项？')
          .then(function () {
            return delDeclarer(declarerId)
          })
          .then(() => {
            this.getList()
            this.$modal.msgSuccess('删除成功')
          })
          .catch(() => {})
      }
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId)
      this.flag = !selection.length
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.flag = true
      this.ids = []
    },
    /** 提交按钮 */
    submitForm() {
      if (this.ids) {
        addDeclarer(this.ids).then((response) => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        })
      } else {
        this.$message.warning('提示：请至少选择一行')
      }
    },
    /** 初始化用户下拉 */
    getUserOptions() {
      getUsers().then((response) => {
        this.userOptions = response.data
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm('editForm')
      const declarerId = row.declarerId
      getDeclarer(declarerId).then((response) => {
        this.editForm = response.data
        this.editOpen = true
        this.editTitle = '编辑 ' + row.nickname + ' 的责任人'
        this.getUserOptions()
      })
    },
    /** 取消按钮 */
    editCancel() {
      this.editOpen = false
      this.editForm = {}
      this.resetForm('editForm')
    },
    /** 提交按钮 */
    editSubmitForm: function () {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          if (this.editForm && this.editForm.principalId) {
            updateDeclarer(this.editForm).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.editOpen = false
              this.getList()
            })
          } else {
            this.$message.warning('提示：责任人不能为空')
          }
        }
      })
    }
  }
}
</script>

<style></style>
